#!/bin/bash

# This script performs a series of Git and LaTeX operations on a specified project directory.
# Usage: ./script_name project_name

# Function to push changes after performing Git and LaTeX operations
push_changes() {
    local project_name=$1
    # Construct the absolute path using $HOME and appending the project directory
    local project_dir="$HOME/Documents/repo-research/$project_name"
    echo "Pushing changes for project in directory: $project_dir"

    # Change to the specified project directory
    cd "$project_dir" || exit 1

    # Perform git latexdiff operation
    git latexdiff --flatten --tmpdirprefix ./gitdiff/ --cleanup keeppdf --ignore-latex-errors --pdf-viewer zathura --latexmk --main "${project_name}.tex" HEAD~1

    # Move and copy the resulting files
    local timestamp=$(date +%y%m%d-%H)
    mv gitdiff/git-latexdiff.* "gitdiff/git-latexdiff-$timestamp"
    cp "${project_name}.pdf" "gitdiff/git-latexdiff-$timestamp/${project_name}-$(date +%Y-%m-%d-%H).pdf"

    # Amend the last commit without changing the commit message
    git add --all && git commit --amend --no-edit

    # Checkout master, merge the changes from 'ganx', and push
    git checkout master && git merge ganx && git push && git checkout ganx

    # Push the changes
    git push
}

# Check if an argument is provided
if [ $# -ne 1 ]; then
    echo "Error: No project name specified."
    echo "Usage: $0 project_name"
    exit 1
fi

# Call the push_changes function with the provided project name
push_changes "$1"
